package cn.tedu.csmall.product.webapi.controller;

import cn.tedu.csmall.commons.response.JsonResult;
import cn.tedu.csmall.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.pojo.vo.CategoryAddNewVO;
import cn.tedu.csmall.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.sevice.ICategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/categories")
@Api(tags = "1. 类别管理模块")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    // http://localhost:8080/categories/add-new?name=JiaDian&parentId=0
    @ApiOperationSupport(order = 10)
    @ApiOperation("增加类别")
    @PostMapping("/add-new")
    public JsonResult<CategoryAddNewVO> addNew(@Valid CategoryAddNewDTO categoryAddNewDTO){
        Long id = categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok(new CategoryAddNewVO().setId(id));
    }

    @ApiOperationSupport(order = 40)
    @ApiOperation("查询类别列表")
    @GetMapping("")
    public JsonResult<List<CategoryListItemVO>> list(){
        return JsonResult.ok(categoryService.list());
    }
}
